package com.hancher.app;

import android.os.Bundle;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.hancher.hrpagemanager.HrPageManager;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Handler;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private HrPageManager<MyViewHolder> prvm1;

    public static class MyViewHolder {
        public MyViewHolder(String name) {
            this.name = name;
        }
        private String name;
        public String getName() {
            return name;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        SmartRefreshLayout refreshLayout = findViewById(R.id.refresh_layout);

        prvm1 = new HrPageManager<MyViewHolder>()
                .setContext(this)
                .setRecyclerView(recyclerView)
                .setAdapter(new BaseQuickAdapter<MyViewHolder, BaseViewHolder>(R.layout.list_item) {
                    @Override
                    protected void convert(BaseViewHolder helper, MyViewHolder item) {
                        helper.setText(R.id.text1, item.getName());
                    }
                })
                .setRefresh(true)
                .setLoadMore(true)
                .setRefreshLayout(refreshLayout)
                .setRefreshListener(new OnRefreshLoadMoreListener() {
                    @Override
                    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                        queryData(HrPageManager.LOADMORE);
                    }

                    @Override
                    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                        queryData(HrPageManager.FRESH);
                    }
                })
                .setDivider(true)
                .build();
        queryData(HrPageManager.FRESH);
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    private void queryData(String type) {
        prvm1.setDataChangeType(type);
        int pageNum = prvm1.getPage();
        int pageSize = prvm1.getSize();
        final List<MyViewHolder> data = new ArrayList<>();
        for (int i = 0; i < pageSize; i++) {
            if (((pageNum - 1) * pageSize + (i+1)) > 33) {
                break;
            }
            data.add(new MyViewHolder("当前数据=" + ((pageNum - 1) * pageSize + (i+1))));
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                prvm1.freshPageData(data, 33);
            }
        }, 3000);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
